--[[
MelonScript
Player Util
--]]

local PMeta = FindMetaTable( "Player" )

-- IC Grammar check.
function PMeta:CheckGrammar( Text )
    local RetText = " " .. string.lower( Text ) .. " "


    RetText = string.Trim( RetText )
    local UpperText = string.upper( string.sub( RetText, 0, 1 ) )
    RetText = UpperText .. "" .. string.sub( RetText, 2, string.len( RetText ) )
	
    if ( !string.find( string.sub( RetText, string.len( RetText ) ), "%p" ) ) then
        RetText = RetText .. "."
    end
    return RetText
end

-- Send chattext to the client.
function PMeta:SendChatText( ... )
	umsg.Start( "SendChatText", self ) 
	umsg.Short( #arg )
		local colour = true
		for k, v in ipairs( arg ) do
			if ( colour ) then
				local vec = Vector( v.r, v.g, v.b )
				umsg.Vector( vec )
			else
				umsg.String( v )
			end
			colour = !colour
		end
	umsg.End()
end

// Before anything I'm going to implement this, if any errors occur put this after PMeta:Nick(), - schu.

function PMeta:ConvertString( update, nwtable )
self:SetPData( tostring(update), tostring(self:GetNWString( tostring(nwtable ) ) ) )
end

function PMeta:ConvertInteger( update, nwtable )
self:SetPData( tostring(update), tonumber( self:GetNWInt( tostring(nwtable) ) ) )
end

function PMeta:Nick()
	return self:GetNWString( "Name" )
end

cc = {}
-- works
function cc.PrintName( pl )
print( pl:Nick() )
end
concommand.Add( "ccm_printnick", cc.PrintName )

-- WORKS
function PMeta:UpdateName()
self:ConvertString( "Name", "Name" )
end

-- WORKS
function cc.UpdateName( pl )
	pl:UpdateName()
end
concommand.Add("ccm_updatename", cc.UpdateName )

-- works
function cc.SetName( pl, cmd, args )
if ( args[1] == nil ) then
	print("CC_SETNAME ERROR: No arguments found(Args[1])!");
end
pl:SetNWString( "Name", args[1] )
pl:UpdateName()
end
concommand.Add( "ccm_setname", cc.SetName )

-- WORKS
function cc.PrintNameTables( pl, cmd, args )
print( "PDATA: " .. pl:GetPData("Name") .. ", NW: " .. pl:GetNWString("Name") )
end
concommand.Add( "ccm_printnametables", cc.PrintNameTables )

-- WORKS
function PMeta:UpdateMoney()
self:ConvertInteger( "Money", "Money" )
end

-- WORKS
function cc.UpdateMoney( pl )
	pl:UpdateMoney()
end
concommand.Add("ccm_updatename", cc.UpdateName )

-- WORKS.
function PMeta:SetMoney( integer )
local n = tonumber(integer)
local s = tostring(integer)
self:SetNWInt("Money", n )
self:UpdateMoney()
print("Set money to " .. s )
end

-- WORKS
function PMeta:GetMoney()
local r = self:GetPData("Money")
return r
end

function PMeta:AddMoney( integer )
local c = self:GetPData("Money")
self:SetNWInt("Money", c + tonumber(integer) )
self:UpdateMoney()
end

function PMeta:SubMoney( integer )
local c = self:GetPData("Money")
self:SetNWInt("Money", c - tonumber(integer) )
self:UpdateMoney()
end

function cc.PrintMoney( pl )
local s1 = pl:GetMoney()
local s2 = pl:GetNWInt("Money")
print( "PDATA: " .. s1 .. " NW: " .. s2 .. "." );
end
concommand.Add( "ccm_printmoney", cc.PrintMoney )

function PMeta:UpdateModel()
self:ConvertString( "Model", "Model" )
end

function PMeta:SetModelString( path )
	self:SetNWString( "Model", tostring(path) )
	self:UpdateModel()
	self:SetModel( tostring(path) )
end

function cc.SetModel( pl, cmd, args )
	pl:SetModelString( args[1] )
end
concommand.Add( "ccm_setmodel", cc.SetModel )

function cc.PrintModel( pl )
	print( pl:GetModel() )
end
concommand.Add( "ccm_printmodel", cc.PrintModel )

function cc.AddMoney( pl, cmd, args )
	pl:AddMoney( args[1] )
end
concommand.Add( "ccm_addmoney", cc.AddMoney )

function cc.SubMoney( pl, cmd, args )
	pl:SubMoney( args[1] )
end
concommand.Add( "ccm_submoney", cc.SubMoney )

function PMeta:IndexExists( index )
if ( self:GetPData( tostring(index) ) == nil ) then
	return false
else
return true
end
end
